function Auth() {
   this.maskWrapper = $('.mask-wrapper');
   this.scrollWrapper = $('.scroll-wrapper');
   this.switch = $('.switch');

}

Auth.prototype.showEvent = function () {
   var self = this;
   self.maskWrapper.show();
}
Auth.prototype.hideEvent = function () {
   var self = this;
   self.maskWrapper.hide();
}
Auth.prototype.loginRegister = function () {
   var self = this;
   $('#btn').click(function (){
       self.showEvent();
       self.scrollWrapper.css({'left': 0});
   });
   $('#register').click(function (){
       self.showEvent();
       self.scrollWrapper.css({'left': -400});
    });
}
Auth.prototype.switchChange = function () {
   var self = this;
   self.switch.click(function (){
       var current = self.scrollWrapper.css('left');
       if(current === "-400px") {
           self.scrollWrapper.animate({'left': 0});
       }else {
           self.scrollWrapper.animate({'left': -400});
       }
    });
}
Auth.prototype.closeLogin = function () {
   var self = this;
    $('.close-wrapper').click(function (){
        self.hideEvent();
    });
}
Auth.prototype.listSignin = function () {
    var self = this;
    var signinGroup = $('.signin-group');
    var telephoneInput = signinGroup.find("input[name='telephone']");
    var passwordInput = signinGroup.find("input[name='password']");
    var rememberInput = signinGroup.find("input[name='remember']");

    var submitBtn = $(".signin-group .submit-btn");
    submitBtn.click(function () {
        var telephone = telephoneInput.val();
        var password = passwordInput.val();
        var remember = rememberInput.prop('checked');
        console.log(telephone)

        xfzajax.post(
            {
                'url': '/account/login/',
                'data': {
                    'telephone': telephone,
                    'password': password,
                    'remember': remember ? 1 : 0
                },
                'success': function (result) {
                    if (result['code'] == 200) {
                        self.hideEvent();
                        window.location.reload()
                        window.messageBox.showSuccess("登陆成功！");
                    } else {
                        var messageObject = result['message'];
                        if (typeof messageObject == 'string' || messageObject.constructor == String) {
                            window.messageBox.show(messageObject);
                        } else {
                            for (var k in messageObject) {
                                var messages = messageObject[k];
                                var message = messages[0];
                                window.messageBox.show(message);
                            }
                        }
                    }
                    console.log(result)
                },
                'fail': function (error) {
                    console.log(error);
                }
            }
        )
    })
}
Auth.prototype.listenSmsCaptchaEvent = function () {
    var self = this;
    var smsCaptcha = $(".sms-captcha-btn");
    var telephoneInput = $(".signup-group input[name='telephone']");
    smsCaptcha.click(function () {
        var telephone = telephoneInput.val();
        if(!telephone){
            messageBox.showInfo('请输入手机号码！');
        }else {
        xfzajax.get({
            'url': '/account/send_message/',
            'data':{
                'telephone': telephone
            },
            'success': function (result) {
                if(result['code'] == 200){
                    messageBox.showInfo('发送成功！');
                    smsCaptcha.css({"color":"#ccc", "border": "1px solid #ccc"})
                }
            },
            'fail': function (error) {
                console.log(error);
            }
        });}
    });
};
Auth.prototype.listenSignupEvent = function () {
    var self = this;
    var signupGroup = $('.signup-group');
    var submitBtn = signupGroup.find('.submit-btn');
    submitBtn.click(function (event) {
        event.preventDefault();
        var telephoneInput = signupGroup.find("input[name='telephone']");
        var usernameInput = signupGroup.find("input[name='username']");
        var imgCaptchaInput = signupGroup.find("input[name='img_captcha']");
        var password1Input = signupGroup.find("input[name='password1']");
        var password2Input = signupGroup.find("input[name='password2']");
        var smsCaptchaInput = signupGroup.find("input[name='sms_captcha']");

        var telephone = telephoneInput.val();
        var username = usernameInput.val();
        var img_captcha = imgCaptchaInput.val();
        var password1 = password1Input.val();
        var password2 = password2Input.val();
        var sms_captcha = smsCaptchaInput.val();

        xfzajax.post({
            'url': '/account/register/',
            'data': {
                'telephone': telephone,
                'username': username,
                'img_captcha': img_captcha,
                'password1': password1,
                'password2': password2,
                'sms_captcha': sms_captcha
            },
            'success': function (result) {
                if (result['code'] == 200) {
                    self.hideEvent();
                    window.location.reload()
                    window.messageBox.showSuccess("注册成功！");
                } else {
                    var messageObject = result['message'];
                    if (typeof messageObject == 'string' || messageObject.constructor == String) {
                        // window.messageBox.show(messageObject);
                        window.messageBox.show("两次密码不一致或验证码短信验证不正确！")
                    } else {
                        for (var k in messageObject) {
                            var messages = messageObject[k];
                            var message = messages[0];
                            // window.messageBox.show(message);
                            window.messageBox.show("两次密码不一致或验证码短信验证不正确！");
                        }
                    }
                }
                console.log(result)
                },
        });
    });
};

Auth.prototype.run = function () {
   this.hideEvent();
   this.loginRegister();
   this.switchChange();
   this.closeLogin();
   this.listSignin();
   this.listenSmsCaptchaEvent();
   this.listenSignupEvent();
}
$(function () {
    auth = new Auth();
    auth.run();
})